﻿using System;
using System.Collections;
using System.Data;
using DotNetNuke.Framework;
using DotNetNuke.Common.Utilities;
using gdlmo.DNN.Modules.Category.Data;

namespace gdlmo.DNN.Modules.Category.Business
{
	public class CategoryController : DotNetNuke.Entities.Modules.ISearchable, DotNetNuke.Entities.Modules.IPortable
	{
		#region "Public Methods"	
		const string CATEGORY_KEY = "gdlmo_Category";
		public CategoryInfo Get(int CategoryID,int moduleId )
		{
			return (CategoryInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetCategory(CategoryID,moduleId), typeof(CategoryInfo));
		}

		public ArrayList List()
		{			
			if(DataCache.GetCache(CATEGORY_KEY)==null)
			{
				ArrayList Categories = DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().ListCategory(), typeof(CategoryInfo));
				DataCache.SetCache(CATEGORY_KEY,Categories) ;
			}
			return (ArrayList)(DataCache.GetCache(CATEGORY_KEY));
		}

		public ArrayList GetByModules(int ModuleID )
		{
			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetCategoryByModules(ModuleID), typeof(CategoryInfo));
		}
	
		public int Add(CategoryInfo objCategory)
		{
			int retVal;			
			retVal = (int)DataProvider.Instance().AddCategory(objCategory.ModuleID, objCategory.Name, objCategory.Description, objCategory.ParentID, objCategory.Depth, objCategory.ViewOrder, objCategory.AdminRoles, objCategory.EditRoles, objCategory.ViewRoles, objCategory.CreatedByUser, objCategory.CreatedDate, objCategory.IsActive);
			DataCache.RemoveCache(CATEGORY_KEY);
			List();
			return retVal;
		}
	
		public void Update(CategoryInfo objCategory)
		{
			DataProvider.Instance().UpdateCategory(objCategory.CategoryID, objCategory.ModuleID, objCategory.Name, objCategory.Description, objCategory.ParentID, objCategory.Depth, objCategory.ViewOrder, objCategory.AdminRoles, objCategory.EditRoles, objCategory.ViewRoles, objCategory.CreatedByUser, objCategory.CreatedDate, objCategory.IsActive);
			DataCache.RemoveCache(CATEGORY_KEY);
			List();
		}
		
		public void Delete(int CategoryID)
		{
			DataProvider.Instance().DeleteCategory(CategoryID);
		}
		#endregion

		#region "Optional Interfaces"		
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(DotNetNuke.Entities.Modules.ModuleInfo ModInfo)
		{
			return null;
		}
	
        public string ExportModule(int ModuleID)
		{
			return "";
        }

        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
		{
		}
		#endregion
	}
}
